package com.example.demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.annotation.JSONField;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author:m1343
 * @Version:1.0
 * @CreateTime:2020/3/5 13:58
 * Description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseResult {

    /**
     * 返回码
     */
    @JSONField(ordinal = 0)
    private int status;
    /**
     * 对应信息
     */
    @JSONField(ordinal = 1)
    private String msg;
    /**
     * Controller 的返回的具体对象信息
     */
    @JSONField(ordinal = 2)
    private Object data;


    public BaseResult(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }


    public static BaseResult success() {
        return new BaseResult(CommonResponseCode.SUCCESS, "请求成功");
    }

    public static BaseResult success(String msg) {
        return new BaseResult(CommonResponseCode.SUCCESS, msg);
    }

    public static BaseResult success(String msg,Object data) {
        return new BaseResult(CommonResponseCode.SUCCESS, msg,data);
    }

    public static BaseResult failure(String msg) {
        return new BaseResult(CommonResponseCode.FAILURE, msg);
    }

    public static BaseResult failure(int status, String msg) {
        return new BaseResult(status, msg);
    }

    /**
     * 封装data数据，返回给前端的数据格式：{"status": 0,"data": {},"msg": "请求成功"}
     *
     * @param data
     * @return json 字符串
     */
    public static BaseResult buildSuccessData(Object data) {
        return new BaseResult(CommonResponseCode.SUCCESS, "请求成功", data);
    }

    /**
     * 封装data数据，返回给前端的数据格式：{"status": 0,"data": {"list":[]},"msg": "请求成功"}
     *
     * @param list
     * @return json 字符串
     */
    public static BaseResult buildSuccessDataList(List list) {
        Map<String, Object> data = new HashMap<>();
        data.put("list", list);
        return new BaseResult(CommonResponseCode.SUCCESS, "请求成功", data);
    }

}